package com.atguigu.transform;

import lombok.val;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.co.CoProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author Mr.Zheng
 * @Date 2023/6/14 19:39
 *
 * connect 合流：
 *      可以支持两条不同类型的流进行connect
 *      实际上两条流没有真正合并到一起，在内部处理过程中，还是区分对待，
 *      但是最终要将两条流的类型进行统一，真正意义上变成一条流。
 *
 */
public class Flink10_ConnecStream {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Integer> ds1 = env.fromElements(1, 2, 3, 4, 5);

        DataStreamSource<Integer> ds2 = env.fromElements(6, 7, 8, 9, 10);

        DataStreamSource<String> ds3 = env.fromElements("a", "b", "c", "d", "e");

        // connect
        ConnectedStreams<Integer, String> connectedStreams = ds1.connect(ds3);
        SingleOutputStreamOperator<String> process = connectedStreams.process(
                new CoProcessFunction<Integer, String, String>() {
                    /**
                     * 处理第一条数据
                     */
                    @Override
                    public void processElement1(Integer value, CoProcessFunction<Integer, String, String>.Context context, Collector<String> out) throws Exception {

                        out.collect(value.toString());
                    }

                    /**
                     * 处理第二条数据
                     */
                    @Override
                    public void processElement2(String value, CoProcessFunction<Integer, String, String>.Context context, Collector<String> out) throws Exception {

                        out.collect(value.toUpperCase());


                    }
                }
        );

        process.print();

        env.execute();
    }
}
